////////////////////////////////////////////////
// -- DepthHUD Lite                           //
// by Hurricaaane (Ha3)                       //
//                                            //
// http://www.youtube.com/user/Hurricaaane    //
//--------------------------------------------//
// Skin                                       //
////////////////////////////////////////////////
local dhlite = dhlite

local surface = surface
local draw = draw
local Color = Color

local SKIN = {}
SKIN.PrintName 		= ""
SKIN.Author 		= ""
SKIN.DermaVersion	= 1

local iBorder = 4
local giBorderExpand = 6

SKIN.cBackground = Color(0, 0, 0, 128)
SKIN.cNormal = Color(192, 255, 0, 192)
SKIN.cBad    = Color(192, 255, 0, 192)

function SKIN:_CalcRoundedSize( iMinSide )
	//return math.floor(iMinSide / 16) * 2
	//return iMinSide >= 16 and 2 or 0
	return 0
	
end

function SKIN:RestoreAlpha( panel )
	self.cBackground.a = 128
	self.cNormal.a = 192
	self.cBad.a = 192
	
end

local matBlurScreen = Material( "pp/blurscreen" )
local matFlare      = Material( "dhlite/beacon_flare_add" )

local bgPoly = {
    { x = 0, y = 0, u = 0, v = 0 },
    { x = 0, y = 0, u = 0, v = 0 },
    { x = 0, y = 0, u = 0, v = 0 },
    { x = 0, y = 0, u = 0, v = 0 }
}

function SKIN:SetupPolygon( panel, xn, yn, xa, ya )
	local x, y, w, h = -xn, -yn, panel:GetWide() + xn + xa, panel:GetTall() + yn + ya
	local x1, y1 = panel:LocalToScreen( x + 1, y + 1 )
	local x2, y2 = panel:LocalToScreen( w - 1, h - 1 )
	
	
	bgPoly[1].x = 1
	bgPoly[1].y = 1
	bgPoly[2].x = w - 1
	bgPoly[2].y = 1
	bgPoly[3].x = w - 1
	bgPoly[3].y = h - 1
	bgPoly[4].x = 1 
	bgPoly[4].y = h - 1
	
	bgPoly[1].u = x1 / ScrW()
	bgPoly[1].v = y1 / ScrH()
	
	bgPoly[2].u = x2 / ScrW()
	bgPoly[2].v = y1 / ScrH()
	
	bgPoly[3].u = x2 / ScrW()
	bgPoly[3].v = y2 / ScrH()
	
	bgPoly[4].u = x1 / ScrW()
	bgPoly[4].v = y2 / ScrH()
	
end

function SKIN:PaintOverHPanel( panel )
    if dhlite:IsEditMode() then
		panel:SetAlpha( 255 )
		surface.SetDrawColor( 255, 255, 255, 128 )
		surface.DrawOutlinedRect( 0, 0, panel:GetWide(), panel:GetTall() )
		
	end
	
end

function SKIN:PaintHPanel( panel )
	/*self:_EvaluateBGAlpha( panel )
	local lx, ly = panel:GetSize()
	local minl = lx < ly and lx or ly
	draw.RoundedBox( self:_CalcRoundedSize( minl ), 0, 0, lx, ly, self.cBackground )*/
	
    local x, y = panel:ScreenToLocal( 0, 0 )
	
	local vis = panel:GetVisibility( )
	local br = math.Round( (1 - vis) * giBorderExpand )
	
	panel:SetAlpha( 255 * vis )
	
	/*surface.SetMaterial( matBlurScreen )
	surface.SetDrawColor( 255, 255, 255, 255 * vis )
	
	matBlurScreen:SetMaterialFloat( "$blur", 20 )
	render.UpdateScreenEffectTexture()
	
	--self:SetupPolygon( panel, br, br, br, br )
	--surface.DrawPoly( bgPoly )
	
	surface.DrawTexturedRect( x, y, ScrW(), ScrH() )*/
	 
	DisableClipping( true )
	surface.SetDrawColor( 0, 0, 0, 64 )
	surface.DrawRect( -br, -br, panel:GetWide() + br * 2, panel:GetTall() + br * 2 )
	 
	// Border
	surface.SetDrawColor( 0, 0, 0, 64 )
	surface.DrawOutlinedRect( -br, -br, panel:GetWide() + br * 2, panel:GetTall() + br * 2 )
	DisableClipping( false )
	self:RestoreAlpha()
	
end

local iFlareMul = 4
local iFlareAlpha = 96

function SKIN:PaintHIndicator( panel )
	local lx, ly = panel:GetSize()
	local bLxIsMin = lx <= ly
	local bLyIsMin = lx >= ly
	local fRate = panel:GetRate()
	local drawlx = bLyIsMin and math.floor(lx * fRate) or lx
	local drawly = bLxIsMin and math.floor(ly * fRate) or ly
	drawlx = drawlx - drawlx % 2
	drawly = drawly - drawly % 2
	local minl = drawlx < drawly and drawlx or drawly
	
	local vis = panel:GetParent():GetVisibility( )
	local bp = math.Round( (1 - vis) * giBorderExpand * fRate )
	
	DisableClipping( true )
	local width, height = drawlx + bp * 2, drawly + bp * 2
	draw.RoundedBox( self:_CalcRoundedSize( minl ), (lx - drawlx) / 2 - bp, (ly - drawly) / 2 - bp, width, height, self.cNormal )
	
	self.cNormal.a = fRate * iFlareAlpha
	surface.SetDrawColor( self.cNormal )
	surface.SetMaterial( matFlare )
	surface.DrawTexturedRectRotated( lx / 2, ly / 2, width * iFlareMul, height * iFlareMul )
	DisableClipping( false )
	self:RestoreAlpha()
	
end

function SKIN:LayoutHLabel( panel )
	panel:SetColor( self.cNormal )
	
end

function SKIN:LayoutHIndicatorLabel( panel )
	local px, py = panel.e_vIndicator:GetParent():GetSize()
	panel.e_vIndicator:SetSize( py * 0.60 - iBorder * 2, py - iBorder * 2 )
	panel.e_vIndicator:AlignLeft( iBorder )
	panel.e_vIndicator:CenterVertical()
	
	panel.e_vMainLabel:SetSize( panel:GetWide() - iBorder * 3 - panel.e_vIndicator:GetWide(), panel:GetTall() )
	panel.e_vMainLabel:AlignRight( iBorder )
	panel.e_vMainLabel:CenterVertical()
	
	panel.e_vSubLabel:SetSize( panel.e_vMainLabel:GetSize() )
	panel.e_vSubLabel:AlignRight( iBorder )
	panel.e_vSubLabel:AlignBottom( 0 )
	
	//debug check what true means as a parameter to invalidate layout and where exactly it should be used
	//panel.e_vIndicator:InvalidateLayout( true )
	
end

derma.DefineSkin( "DHLITE_Skin" .. DHLITE_DEBUG_SKINGENERATOR, "", SKIN )
